package com.itheima.health.task;

import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Slf4j
@Component
public class AliOssTask {

    @Resource
    private SetmealDao setmealDao;
    @Resource
    private AliOssUtil aliOssUtil;

    @Scheduled(cron = "* 0 * * * ?")
    public void removeUselessFile(){
        List<String> imageList = aliOssUtil.getImageList();
        //遍历各个文件名
        for (String s : imageList) {
            //根据图片路径查找数据库,并返回该数据
            Setmeal setmeal = setmealDao.selectbyImage(s);
            //如果该文件名未查找到数据,代表该图片是垃圾文件
            if (Objects.isNull(setmeal)){
                aliOssUtil.deleteFile(s);
                log.info("已从阿里云删除垃圾文件:{}",s);
            }
        }
    }
}
